/**
 * Created by wxm-pc on 2017/8/16.
 */
var PAGE_INDEX = 1;
$(document).ready(function () {
    // 获取用户名
    var userInfoPath = "user/info/get";
    var userInfoParams = {
        "SESSION_ID":SESSION_ID
    };
    request(userInfoPath,userInfoParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(flg) {
            $("#nickname").text(res.BODY.USER_NAME ? res.BODY.USER_NAME : "未知");
        }
    })
    //获取总积分
    $("#content").empty();
    var pointPath = "points/deatil";
    var pointParams = {
        "SESSION_ID":SESSION_ID
    };
    request(pointPath,pointParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(flg) {
            $("#usable_point").text(res.BODY.USABLE_POINTS);
            if(res.BODY.USABLE_POINTS == 0) $(".addr-constellation").css("display","none");
        }
    });
    var pointListPath = "points/change/list";
    var pointListParams = {
        "SESSION_ID":SESSION_ID,
        "PAGE_CAPACITY":10,
        "PAGE_INDEX":PAGE_INDEX
    };
    request(pointListPath,pointListParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(flg) {
            getPointList(res);
        }
    })
});

//获取历史积分记录
function getPointList(res) {
    var orderHtml =
        `<div class="history-order">
            <div class="item-text">
                <div class="hist-title">成功支付订单</div>
                <div class="hist-date">%s</div>
            </div>
            <div class="edit-item">
                <span class="edit-text">+%s分</span>
            </div>
        </div>`;
    _.each(res.BODY.LIST,function (item, index) {
        $("#content").append(sprintf(orderHtml,item.CREATETIME,item.CHANGE_POINTS));
        off_on = true; //[重要]这是使用了 {滚动加载方法1}  时 用到的 ！！！[如果用  滚动加载方法1 时：off_on 在这里不设 true的话， 下次就没法加载了哦！]
    });
    PAGE_INDEX++;  //请求成功下次加载页码加1
};

//下拉加载
function pullDownLoad() {
    var pointListPath = "points/change/list";
    var pointListParams = {
        "SESSION_ID":SESSION_ID,
        "PAGE_CAPACITY":10,
        "PAGE_INDEX":PAGE_INDEX
    };
    request(pointListPath,pointListParams,function (res) {
        var flg = /^0+$/ig.test(res.RSPCD); //全零true
        if(flg) {
            getPointList(res);
        }
    })
};
$('.page').scroll(function() {
    //当时滚动条离底部60px时开始加载下一页的内容
    if (($(this)[0].scrollTop + $(this).height() + 60) >= $(this)[0].scrollHeight) {
        //这里用 [ off_on ] 来控制是否加载 （这样就解决了 当上页的条件满足时，一下子加载多次的问题啦）
        if (off_on) {
            off_on = false;
            //page++;
            console.log("第"+PAGE_INDEX+"页");
            pullDownLoad();  //调用执行上面的加载方法
        }
    }
});